<?
session_start();
require_once("../includes/public.inc.php");
$openid = isset($_SESSION['openid'])?$_SESSION['openid']:'oYqwzuFX-ue9vd9Ws8d1cl3xEo90';
if ($openid)
{
	$openusers =  mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where openid='".$openid."' order by id desc limit 1"));
	//print_r($openusers);exit;
	if ($openusers)
	{
		//进入页面不做其他动作
		if (!$_SESSION['openid'])
		{
			$_SESSION['nickname'] = $subscribe['nickname'];
			$_SESSION['openid'] = $subscribe['openid'];
		}
	}
	else //记录信息
	{
		$intouser = mysql_query("insert into ".DBQIAN."users (username,name,sex,country,province,city,credit,createtime,openid,headimg_url,last_login,subscribe) values ('".$subscribe['nickname']."','".$subscribe['nickname']."','".$subscribe['sex']."','".$subscribe['country']."','".$subscribe['province']."','".$subscribe['city']."',0,'".date('y-m-d H:i:s',time())."','".$subscribe['openid']."','".$subscribe['headimgurl']."','".$subscribe['subscribe_time']."',".$subscribe['subscribe'].")");//增加次数
		$_SESSION['nickname'] = $subscribe['nickname'];
		$_SESSION['openid'] = $subscribe['openid'];
	}
	//判断今天是否签到
	$qiandaoinfo =  mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where openid='".$openid."' order by id desc limit 1"));
	$qiandao = mysql_fetch_assoc(mysql_query("select * from ".DBQIAN."qiandao where openid='".$openid."' order by time desc limit 1"));// and time=>'".date('Y-m-d')."' and time<='".date('Y-m-d')." 23:59:59'
	if($qiandao)
	{
		$nowtime=date('Y-m-d',time());
		if($nowtime<=$qiandao['time']&&$qiandao['time']<=$nowtime.' 23:59:59')
		{
			echo "done";exit;
		}
		else
		{
			$continue = mysql_fetch_assoc(mysql_query("select * from ".DBQIAN."qiandao where openid='".$openid."' and time=>'".$yesterday."' and time<='".$yesterday." 23:59:59' order by time desc limit 1"));// and time=>'".date('Y-m-d')."' and time<='".date('Y-m-d')." 23:59:59'
			$sql="insert into ".DBQIAN."qiandao (users_id,time,credit,quantity,status,openid) values(".$qiandaoinfo['id'].",'".date('Y-m-d H:i:s',time())."',0.02,quantity+1,1,'".$qiandaoinfo['openid']."')";
			$create = mysql_query($sql);
			$create_id = mysql_insert_id();
			if ($create_id)
			{
				//$quantity=$qiandao['quantity']+1;//以时间排序，取最近一条记录
				$quantity=$qiandaoinfo['qiandao_quantity']+1;
				$updatesql= "update ".DBQIAN."users SET qiandao_quantity =" . $quantity .",credit=(credit+0.02) where openid ='".$openid."'";
				$create_query = mysql_query($updatesql);
				if ($create_query)
				{
					//以后需要判断是否连续签到
					$yesterday = date("Y-m-d",strtotime(" -1 day"));//昨天
					//$tomorrow = date("Y-m-d ",strtotime(" +1 day")); //明天
					if ($continue)//连续签+1
					{
						$c_update = "update ".DBQIAN."users SET continuous=(continuous+1)  where openid ='".$openid."'";
					}
					else //否则=1
					{
						$c_update = "update ".DBQIAN."users SET continuous=1  where openid ='".$openid."'";
					}
					echo "success";exit;
				}
				else 
				{
					$deletesql= "delete from ".DBQIAN."qiandao where id =".$create_id;
					$delete_query = mysql_query($deletesql);
					echo "failure";exit;
				}
			}
			else 
			{
				echo "failure";exit;
			}
		}
	}
	else
	{
		$sql="insert into ".DBQIAN."qiandao (users_id,time,credit,quantity,status,openid) values(".$qiandaoinfo['id'].",'".date('Y-m-d H:i:s',time())."',0.02,quantity+1,1,'".$qiandaoinfo['openid']."')";
		$create = mysql_query($sql);
		$create_id = mysql_insert_id();
		if ($create_id)
		{
			//$quantity=$qiandao['quantity']+1;//以时间排序，取最近一条记录
			$quantity=$qiandaoinfo['qiandao_quantity']+1;
			$credit=$qiandaoinfo['credit']+0.02;
			$updatesql= "update ".DBQIAN."users SET qiandao_quantity =". $quantity .",credit=".$credit.",continuous=1 where openid ='".$openid."'";
			$create_query = mysql_query($updatesql);
			if ($create_query)
			{
				echo "success";exit;
			}
			else 
			{
				$deletesql= "delete from ".DBQIAN."qiandao where id =".$create_id;
				$delete_query = mysql_query($deletesql);
				echo "failure";exit;
			}
		}
		else 
		{
			echo "failure";exit;
		}
	}
}
else 
{
	echo "<script>alert('非法进入，请检查提交的数据是否正确!!');</script>";exit;
}
//判断用户是否存在
/*
 * 签到累积当月有效，一个月后自动清空，统计签到次数，累积签到积分到users表
 * */

